package com.fy56.platform.logistics.service.impl;

import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.common.ResultStatus;
import com.fy56.platform.logistics.mapper.ColumnExtMapper;
import com.fy56.platform.logistics.model.ColumnExt;
import com.fy56.platform.logistics.service.web.IColumnExtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by sxg on 2018/8/7.
 */
@Service
public class ColumnExtServiceImpl implements IColumnExtService {

    @Autowired
    private ColumnExtMapper columnExtMapper;

    @Override
    public ColumnExt selectByPrimaryKey(Long id) {
        return columnExtMapper.selectByPrimaryKey(id);
    }

    @Override
    public ColumnExt get(ColumnExt entity) {
        return null;
    }

    @Override
    public ResultModel insert(ColumnExt entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel insertSelective(ColumnExt entity) {
        ResultModel result = null;
        int r = columnExtMapper.insertSelective(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    public ResultModel update(ColumnExt entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel updateSelective(ColumnExt entity) {
        ResultModel result = null;
        int r = columnExtMapper.updateByPrimaryKeySelective(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    public ResultModel delete(Long id) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel batchDelete(List<ColumnExt> list) {
        LinkedList<String[]> failedIds = new LinkedList<String[]>();
        if(null != list && !list.isEmpty()){
            int r = 0;
            for(ColumnExt entity : list){
                r = columnExtMapper.updateDeleteFlagByPrimaryKey(entity);
                if (r == 0) {
                    failedIds.add(new String[]{entity.getId().toString(), "批量禁用失败!"});
                }
            }
        }
        if(failedIds.size() > 0){
            return new ResultModel(0, "", failedIds);
        }else{
            return ResultModel.ok();
        }
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel delete(ColumnExt entity) {
        ResultModel result = null;
        int r = columnExtMapper.updateDeleteFlagByPrimaryKey(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    public List<ColumnExt> findList(ColumnExt columnExt) {
        return null;
    }

    /*
     * @parameter
     * type
     * */
    @Override
    public List<ColumnExt> findColumnExtList(Map<String,Object> par) {
        return columnExtMapper.findColumnExtList(par);
    }

    /**
     * 获取扩展字段
     *
     * @param companyName,columnKey,useFlag
     * @return
     */
    @Override
    public ColumnExt queryColumnExt(Map<String,Object> par) {
        return columnExtMapper.queryColumnExt(par);
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel deleteColumnExt(Long id) {
        int r = columnExtMapper.deleteByPrimaryKey(id);
        if (r == 0){
            return new ResultModel(ResultStatus.DELETE_ERR);
        }
        return ResultModel.ok();
    }
}
